using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using eee.Sheffield.PZ.Math;
using eee.Sheffield.PZ.Imaging.ImageFilter;

namespace eee.Sheffield.PZ.QAAS
{
    public partial class MedianFilterControl : FilterControl
    {
        #region Fields
        private int _kernelSize;
        private int _percentile;
        private PZMath_matrix _ROIMatrix;
        private string _maskImageFileName = "";
        private Bitmap _maskImage;
        #endregion

        #region Constructor
        public MedianFilterControl()
        {
            InitializeComponent();            
            _flag = "Median Filter";
        } 
        public MedianFilterControl(MainForm mainForm, ImageDoc imageDoc)
            : base(mainForm, imageDoc)
        {
            InitializeComponent();            
            _flag = "Median Filter";
        }        
        #endregion

        public override void Reload()
        {
            _maskImageFileName = "";
            _maskImage = null;
            _ROIMatrix = null;
            pictureBoxMaskImage.Image = null;
            base.Reload();
        }

        /// <summary>
        /// check ROI
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBoxROI_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxROI.Checked)
            {
                // search current folder for mask image (*mask*.bmp)
                string path = Directory.GetCurrentDirectory();
                try
                {
                    _maskImageFileName = (Directory.GetFiles(path, "*mask*.bmp"))[0];
                }                
                finally
                {
                    if (_maskImageFileName == "")
                        // display mask image file link label...
                    {
                        //_maskImageFileName = "Choose a mask image...";
                        //linkLabelMaskImageFileName = _maskImageFileName;
                        linkLabelMaskImageFileName.Visible = true;
                    }
                    else
                        // display mask image
                    {
                        linkLabelMaskImageFileName.Text = Path.GetFileName(_maskImageFileName);
                        ShowMaskImage(_maskImageFileName);
                    }                    
                }
            }
            else
            {
                linkLabelMaskImageFileName.Visible = false;
            }
        }

        /// <summary>
        /// click mask image link label 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkLabelMaskImageFileName_LinkClicked(object sender, EventArgs e)
        {
            OpenMaskImage();
        }

        /// <summary>
        /// click button Apply
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonApply_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (checkBoxROI.Checked)
            // mask image exists
            {
                if (_maskImageFileName == "")
                {
                    if (!OpenMaskImage())
                        return;
                }

                _kernelSize = (int)numericUpDownKernelSize.Value;
                _percentile = (int)numericUpDownPercentile.Value;
                _ROIMatrix = GetROIMatrix(_maskImage);

                // median filter
                Median filter = new Median(_kernelSize, _percentile, _ROIMatrix);
                Bitmap dstImage = filter.ApplyGraylevelImageFilter(_imageDoc.Image);
                string caption = "kernel size: " + _kernelSize + " percentil: " + _percentile;
                eee.Sheffield.PZ.Imaging.Image.StandardizeImageTo8bbpIndexed(ref dstImage);
                ShowImage(dstImage, caption);
                _log = "Median filter: " + caption;
                Log(_log);
            }
            else
                // no mask image
            {
                _kernelSize = (int)numericUpDownKernelSize.Value;
                _percentile = (int)numericUpDownPercentile.Value;

                // median filter
                Median filter = new Median(_kernelSize, _percentile);
                Bitmap dstImage = filter.ApplyGraylevelImageFilter(ImageDoc.Image);
                string caption = "kernel size: " + _kernelSize + " percentil: " + _percentile;
                eee.Sheffield.PZ.Imaging.Image.StandardizeImageTo8bbpIndexed(ref dstImage);
                ShowImage(dstImage, caption);
                _log = "Median filter: " + caption;
                Log(_log);
            }

            this.Cursor = Cursors.Arrow;
        }

        /// <summary>
        /// click button Cancle
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCancle_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        #region utility methods
        /// <summary>
        /// show mask image
        /// </summary>
        /// <param name="fileName"></param>
        private void ShowMaskImage(string fileName)
        {
            _maskImage = new Bitmap(fileName);
            pictureBoxMaskImage.Image = _maskImage;
        }

        /// <summary>
        /// open mask image
        /// </summary>
        /// <returns></returns>
        private bool OpenMaskImage()
        {
            if (openFileDialogMaskImage.ShowDialog() == DialogResult.OK)
            {
                _maskImageFileName = openFileDialogMaskImage.FileName;
                try
                {
                    ShowMaskImage(_maskImageFileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Cannot load mask image...: " + ex.Message);
                }
                finally
                {
                    _maskImageFileName = "";
                }
            }
            if (_maskImageFileName == "")
                return false;
            else
                return true;
        }

        /// <summary>
        /// get ROI (mask) matrix
        /// </summary>
        /// <param name="maskImage"></param>
        private PZMath_matrix GetROIMatrix(Bitmap maskImage)
        {
            Bitmap srcImage = (Bitmap)maskImage.Clone();
            eee.Sheffield.PZ.Imaging.Image.StandardizeImageTo8bbpIndexed(ref srcImage);
            ConvertBitmapMatrix converter = new ConvertBitmapMatrix();
            PZMath_matrix ROIMatrix = converter.GetROIMatrix(srcImage);
            return ROIMatrix;
        }
        #endregion
    }
}
